<template>
  <div>
    <el-button type="primary" @click="centerDialogVisible=true" style="float:left;margin-bottom: 5px" size="small">新增</el-button>
<!--  stripe斑马线效果  -->
    <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        stripe
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
          prop="name"
          label="名称"
          sortable
          >
      </el-table-column>
      <el-table-column
          prop="perms"
          label="权限编码"
          sortable
         >
      </el-table-column>
      <el-table-column
          prop="icon"
          label="图标">
      </el-table-column>
      <el-table-column
          prop="type"
          label="类型">
<!--        插槽-->
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.type === 0" type="success">目录</el-tag>
          <el-tag size="small" v-else-if="scope.row.type === 1" type="info">菜单</el-tag>
          <el-tag size="small" v-else-if="scope.row.type === 2" type="primary">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="path"
          label="菜单URL">
      </el-table-column>
      <el-table-column
          prop="component"
          label="菜单组件">
      </el-table-column>
      <el-table-column
          prop="orderNum"
          label="排序号">
      </el-table-column>
      <el-table-column
          prop="statu"
          label="状态">
        <template slot-scope="scope">
          <el-tag  size="small" v-if="scope.row.statu === 1" type="success">正常</el-tag>
          <el-tag size="small"  v-else-if="scope.row.statu === 0" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="operation"
          label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="editHandle(scope.row)">编辑</el-button>
<!--          垂直分割线-->
          <el-divider direction="vertical"></el-divider>
          <template>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="deleteHandle(scope.row.id)">
              <el-button slot="reference" size="mini" type="text" >删除</el-button>
            </el-popconfirm>
          </template>

        </template>
      </el-table-column>
    </el-table>
<!--新增弹窗-->
    <el-dialog
        title="新增菜单"
        :visible.sync="centerDialogVisible"
        width="550px"
        @closed="resetForm('editForm')"
        center>

      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="上级菜单" prop="parentId" >
          <el-select v-model="editForm.parentId" placeholder="请选择上级菜单">
            <template v-for="item in tableData">
              <el-option :label="item.name" :value="item.id"></el-option>
              <template v-for="child in item.children">
                <el-option :label="child.name" :value="child.id">
                  <span>{{"-- "+child.name}}</span>
                </el-option>
              </template>
            </template>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单名称" prop="name" >
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>

        <el-form-item label="权限编码" prop="perms">
          <el-input v-model="editForm.perms"></el-input>
        </el-form-item>

        <el-form-item label="图标" prop="icon">
          <el-input v-model="editForm.icon"></el-input>
        </el-form-item>

        <el-form-item label="菜单URL" prop="path">
          <el-input v-model="editForm.path"></el-input>
        </el-form-item>

        <el-form-item label="菜单组件" prop="component">
          <el-input v-model="editForm.component"></el-input>
        </el-form-item>

        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="editForm.type">
            <el-radio :label=0>目录</el-radio>
            <el-radio :label=1>菜单</el-radio>
            <el-radio :label=2>按钮</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="状态" prop="statu">
          <el-radio-group v-model="editForm.statu">
            <el-radio :label=1>正常</el-radio>
            <el-radio :label=0>禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="排序号" prop="orderNum">
          <el-input-number v-model="editForm.orderNum" :min="1" label="排序号" ></el-input-number>
        </el-form-item>

        <el-form-item style="margin-left: 60px">
          <el-button type="primary" @click="submitForm('editForm')">提交</el-button>
          <el-button @click="resetForm('editForm')">取消</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>

  </div>


</template>

<script>
export default {
  name: "Menu",
  data(){
    return{
      centerDialogVisible: false,
      editForm:{
      },

      tableData: [],
      editFormRules: {
        parentId: [
          {required: true, message: '请选择上级菜单', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        perms: [
          {required: true, message: '请输入权限编码', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        orderNum: [
          {required: true, message: '请填入排序号', trigger: 'blur'}
        ],
        statu: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.getMenuTree()
  },
  methods: {
    //获取菜单树
    getMenuTree(){

      this.$axios.get("/sys/menu/list").then(res => {
        console.log("menu tableData")
        console.log(res)
        this.tableData = res.data.data;
      })
    },

    //编辑表单项
    editHandle(info){
      console.log(info)
      this.$axios.get('/sys/menu/info/'+info.id).then(res => {
        this.editForm = res.data.data
        this.centerDialogVisible = true
      })
    },
    //删除表单项
    deleteHandle(id){
      this.$axios.get('/sys/menu/delete/'+id).then(res =>  {
        console.log(res)
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose: () => {
            this.getMenuTree()
          }
        });
      })
    },
    //编辑和新增使用的是同一提交请求
    submitForm(formName){
      this.$refs[formName].validate((valid)=>{
        if(valid){
            this.$axios.post('/sys/menu/'+(this.editForm.id? "update":"save"),this.editForm).then(res => {
              console.log("新增表单数据")
              console.log(res.data)
              this.resetForm(formName)
              this.$message({
                showClose: true,
                message: '恭喜你，操作成功',
                type: 'success',
                onClose: () => {
                  this.getMenuTree()
                }
              });
            })
        }else{
          console.log('error submit!!');
          return false;
        }
        this.centerDialogVisible = false;
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.editForm = {}
      this.centerDialogVisible = false
    }
  }
}
</script>

<style scoped>

</style>